/**
 * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.weiyi.chat.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.weiyi.chat.R;
import com.weiyi.chat.api.IResetPsw;
import com.weiyi.chat.common.Constant;
import com.weiyi.chat.common.base.BaseActivity;
import com.weiyi.chat.presenter.ResetPswPresenter;
import com.weiyi.chat.utils.PreferenceUtil;
import com.weiyi.chat.utils.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Login screen
 */
public class ResetPswActivity extends BaseActivity implements IResetPsw.Views {

    @BindView(R.id.username)
    EditText mUsername;
    @BindView(R.id.old_psw)
    EditText mOldPsw;
    @BindView(R.id.new_psw)
    EditText mNewPsw;
    @BindView(R.id.confirm_password)
    EditText mConfirmPassword;
    @BindView(R.id.reset_psw)
    Button mResetPsw;

    private boolean mSmsLigin = false;
    private ResetPswPresenter mResetPswPresenter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_reset_psw;
    }

    @Override
    protected void initView() {
        mResetPswPresenter = new ResetPswPresenter(this);
    }

    @Override
    protected void initData() {

    }


    @Override
    public void onResetSucess() {
        ToastUtil.showTextToast(mAct, "密码重置成功");
        finish();
    }

    @Override
    public void showError(String msg) {
        ToastUtil.showTextToast(mAct, msg);
        if (Constant.TOKEN_FIAL.equals(msg)){
            ToastUtil.showTextToast(mAct, "请重新登录");
            PreferenceUtil.clearAll(mAct);
            toLogin();
        }
    }


    @OnClick(R.id.reset_psw)
    public void onViewClicked() {

        final String username = mUsername.getText().toString().trim();
        final String old_psw = mOldPsw.getText().toString().trim();
        String new_psw = mNewPsw.getText().toString().trim();
        String confirm_pwd = mConfirmPassword.getText().toString().trim();
        if (TextUtils.isEmpty(username)) {
            Toast.makeText(this, getResources().getString(R.string.User_name_cannot_be_empty), Toast.LENGTH_SHORT).show();
            mUsername.requestFocus();
            return;
        } else if (TextUtils.isEmpty(old_psw)) {
            Toast.makeText(this, getResources().getString(R.string.old_psw_cannot_be_empty), Toast.LENGTH_SHORT).show();
            mOldPsw.requestFocus();
            return;
        } else if (TextUtils.isEmpty(new_psw)) {
            Toast.makeText(this, getResources().getString(R.string.new_psw_cannot_be_empty), Toast.LENGTH_SHORT).show();
            mNewPsw.requestFocus();
            return;
        } else if (TextUtils.isEmpty(confirm_pwd)) {
            Toast.makeText(this, getResources().getString(R.string.Confirm_password_cannot_be_empty), Toast.LENGTH_SHORT).show();
            mConfirmPassword.requestFocus();
            return;
        } else if (!new_psw.equals(confirm_pwd)) {
            Toast.makeText(this, getResources().getString(R.string.Two_input_password), Toast.LENGTH_SHORT).show();
            return;
        }

        mResetPswPresenter.requsetResetPsw(username, old_psw, new_psw);
    }
}
